 #define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int dx[4] = { 0, 0, -1, 1 };
    int dy[4] = { 1, -1, 0, 0 };
    int nearestExit(vector<vector<char>>& maze, vector<int>& e) {
        int m = maze.size(), n = maze[0].size();

        bool vis[m][n];//标识走过和未走过的区域
        memset(vis, 0, sizeof(vis));

        queue<pair<int, int>> q;
        q.push({ e[0], e[1] });
        vis[e[0]][e[1]] = true;

        int step = 0;//表示走过的距离
        while (q.size())
        {
            step++;
            int sz = q.size();

            for (int i = 0; i < sz; i++)
            {
                auto [a, b] = q.front();
                q.pop();
                for (int j = 0; j < 4; j++)
                {
                    int x = a + dx[j], y = b + dy[j];
                    if (x >= 0 && x < m && y >= 0 && y < n && vis[x][y] != true && maze[x][y] == '.')
                    {
                        if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return step;
                        q.push({ x, y });
                        vis[x][y] = true;
                    }
                }
            }
        }

        return -1;

    }
};

class Solution {
public:
    int minMutation(string startGene, string endGene, vector<string>& bank) {
        unordered_set<string> hash(bank.begin(), bank.end());
        unordered_set<string> vis;//标识是否访问过
        string chs = "ACGT";

        //特殊情况
        if (startGene == endGene) return 0;
        if (!hash.count(endGene)) return -1;

        queue<string> q;
        q.push(startGene);
        vis.insert(startGene);

        int ret = 0;
        while (q.size())
        {
            ret++;
            int sz = q.size();
            while (sz--)
            {
                string s = q.front();
                q.pop();
                for (int i = 0; i < 8; i++)
                {
                    string tmp = s;
                    for (int j = 0; j < 4; j++)
                    {
                        tmp[i] = chs[j];
                        if (hash.count(tmp) && !vis.count(tmp))
                        {
                            if (tmp == endGene) return ret;
                            q.push(tmp);
                            vis.insert(tmp);
                        }
                    }
                }
            }
        }
        return -1;
    }
};

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> hash(wordList.begin(), wordList.end());
        unordered_set<string> vis;//标识是否访问过

        //特殊情况
        if (beginWord == endWord) return 1;
        if (!hash.count(endWord)) return 0;

        queue<string> q;
        q.push(beginWord);
        vis.insert(beginWord);

        int ret = 1;
        while (q.size())
        {
            ret++;
            int sz = q.size();
            while (sz--)
            {
                string s = q.front();
                q.pop();
                for (int i = 0; i < s.size(); i++)
                {
                    string tmp = s;
                    for (char ch = 'a'; ch <= 'z'; ch++)
                    {
                        tmp[i] = ch;
                        if (hash.count(tmp) && !vis.count(tmp))
                        {
                            if (tmp == endWord) return ret;
                            q.push(tmp);
                            vis.insert(tmp);
                        }
                    }
                }
            }
        }
        return 0;
    }
};

class Solution {
public:
    int m, n;
    int cutOffTree(vector<vector<int>>& forest) {
        m = forest.size(), n = forest[0].size();

        //1.找出砍树的顺序
        vector<pair<int, int>> trees;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (forest[i][j] > 1) trees.push_back({ i, j });

        //2.对需要砍的树进行排序
        sort(trees.begin(), trees.end(), [&](const pair<int, int>& p1, const pair<int, int>& p2) {
            return forest[p1.first][p1.second] < forest[p2.first][p2.second];
            });

        //3.按照顺序砍树
        int sx = 0, sy = 0;
        int ret = 0;
        for (auto& [a, b] : trees)
        {
            int step = bfs(forest, sx, sy, a, b);
            if (step == -1) return -1;
            ret += step;
            sx = a, sy = b;
        }
        return ret;
    }

    int dx[4] = { 0, 0, -1, 1 };
    int dy[4] = { 1, -1, 0 ,0 };
    bool vis[51][51];

    int bfs(vector<vector<int>>& f, int sx, int sy, int ex, int ey)
    {
        if (sx == ex && sy == ey) return 0;
        memset(vis, 0, sizeof(vis));//清空上次砍树的数据

        queue<pair<int, int>> q;
        q.push({ sx, sy });
        vis[sx][sy] = true;

        int step = 0;
        while (q.size())
        {
            step++;
            int sz = q.size();
            while (sz--)
            {
                auto [a, b] = q.front();
                q.pop();
                for (int i = 0; i < 4; i++)
                {
                    int x = a + dx[i], y = b + dy[i];
                    if (x >= 0 && x < m && y >= 0 && y < n && f[x][y] && !vis[x][y])
                    {
                        if (x == ex && y == ey) return step;
                        q.push({ x, y });
                        vis[x][y] = true;
                    }
                }
            }
        }

        return -1;
    }
};